﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AteneoLibriBusinessLogic.Entities;
using AteneoLibriBusinessLogic.MyException;

namespace AteneoLibriBusinessLogic.DAL
{
    public class BookFacade
    {
        AteneoLibriEntityFramework.AteneoLibriEntities db = new AteneoLibriEntityFramework.AteneoLibriEntities();

        public List<Book> getBooks()
        {
            List<Book> books = new List<Book>();
            List<AteneoLibriEntityFramework.Book> booksEF = db.Books.ToList();

            foreach (var item in booksEF)
            {
                var book = TOA.BookAssembler.Assembler(item);
                books.Add(book);
            }

            return books;
        }

        public Book getBookById(Guid idBook)
        {
            AteneoLibriEntityFramework.Book bookEF = db.Books.Where(x => x.IdBook == idBook).FirstOrDefault();
            Book book = null;
            if (bookEF != null)
            {
                book = TOA.BookAssembler.Assembler(bookEF);
            }
            return book;
        }

        public bool insertBook(Book book)
        {
            bool result = false;
            try
            {
                AteneoLibriEntityFramework.Book bookEF = TOA.BookAssembler.DeAssembler(book);
                db.Books.AddObject(bookEF);
                db.SaveChanges();

                result = true;
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }

            return result;
        }

        public bool updateBook(Book book)
        {
            bool result = false;
            try
            {
                AteneoLibriEntityFramework.Book bookEF = db.Books.Where(x => x.IdBook == book.IdBook).FirstOrDefault();
                //saleEF = TOA.SaleAssembler.DeAssembler(sale);
                TOA.BookAssembler.WriteToEntity(bookEF, book);

                db.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
                result = true;
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }

            return result;
        }

        public bool deleteBooks(Guid userId)
        {
            bool result = false;
            try
            {
                List<AteneoLibriEntityFramework.Book> books = db.Books.Where(x => x.UserId == userId).ToList();

                foreach (var item in books)
                {
                    db.Books.DeleteObject(item);
                }
                db.SaveChanges();
                result = true;
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }

            return result;
        }


        public bool deleteBook(Guid bookId)
        {
            bool result = false;
            try
            {
                AteneoLibriEntityFramework.Book book = db.Books.Where(x => x.IdBook == bookId).FirstOrDefault();

                if (book != null)
                {
                    db.Books.DeleteObject(book);
                    db.SaveChanges();
                    result = true;
                }
                
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }

            return result;
        }
    }
}
